package com.bbx.taxi.client.Task;

import android.app.Activity;

import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.passanger.PayBuild;
import com.bbx.api.sdk.model.passanger.RechargeBuild;
import com.bbx.api.sdk.model.passanger.Return.Recharge;
import com.bbx.api.sdk.model.passanger.Return.pay.Pay;
import com.bbx.api.sdk.net.base.BaseNetwork;
import com.bbx.api.sdk.net.passeger.conn.RechargeNet;
import com.bbx.api.sdk.net.passeger.conn.pay.PayNet;
import com.bbx.taxi.client.Bean.Message.ObserverListener;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Util.VersionUtils;
import com.bbx.taxi.client.pay.alipay.PayAlipay;
import com.bbx.taxi.client.pay.weixin.PayWeixin;
import com.bbx.taxi.client.wxapi.WXPayEntryActivity;

import org.json.JSONObject;

/**
 * 作者：严旭文 on 2016/12/30 15:27
 * 邮箱：420255048@qq.com
 * 订单支付
 */
public class MyOrderPayTask extends BaseTask{
    // 支付完成接口,由于支付完成通知要经过第三方完成，才能通知，所以要弄个监听，而不能在用noNotify在通知了
    public interface OnPayFinishListener {
        public void onPayFinish(boolean isSuccess);
    }

    public static OnPayFinishListener mOnPayFinishListener = null;

    public static void setOnPayFinishListener(OnPayFinishListener l) {
        mOnPayFinishListener = l;
    }
    public enum GATEWAY{
        weixin,alipay,line,yue
    }
    public ObserverListener.DATA_TYPE TYPE= ObserverListener.DATA_TYPE.PAY;
    PayBuild mPayBuild;
    private Activity context;
    private GATEWAY gateway;
    public MyOrderPayTask(Activity context,GATEWAY gateway,String order_id, String driver_id,int cashAmt) {
        super(context);
        this.context=context;
        MyApplication myApplication = MyApplication.getInstance();
        // 访问服务器获取支付订单
        mPayBuild = new PayBuild(context);
        mPayBuild.uid = myApplication.getUid();
        mPayBuild.access_token = myApplication.getToken();
        mPayBuild.order_id = order_id;
        mPayBuild.gateway = "line";
        mPayBuild.driver_id = driver_id;
        this.gateway=gateway;
        switch (gateway){
            case weixin:
                mPayBuild.cashAmt="0.01";
                mPayBuild.orderType="1";
                switch(VersionUtils.getVersionType(context)){
                    case bbx:
                        mPayBuild.gateway="weixin";
                        break;
                    case tianshang:
                        mPayBuild.gateway="wx_ts";
                        break;
                }
                break;
            case alipay:
                mPayBuild.cashAmt = "0.01";
                mPayBuild.orderType = "1";
                switch(VersionUtils.getVersionType(context)){
                    case bbx:
                        mPayBuild.gateway="alipay";
                        break;
                    case tianshang:
                        mPayBuild.gateway="alipay_ts";
                        break;
                }
                break;
            case line:
                mPayBuild.cashAmt = cashAmt+"";
                mPayBuild.orderType = "2";
                mPayBuild.gateway="line";
                break;
            case yue:
                mPayBuild.cashAmt = cashAmt+"";
                mPayBuild.orderType = "4";
                mPayBuild.gateway="bbxpay";
                break;

        }
    }
    @Override
    public boolean isShow() {
        return true;
    }
    @Override
    public boolean isReturnString() {
        return false;
    }
    @Override
    public boolean condition() {
        // TODO Auto-generated method stub
        return true;
    }
    public void request() {
        setType(TYPE,mPayBuild);
    }
    @Override
    public void success(int status,Object object) {
        switch (gateway){
            case weixin:
                try {
                Pay mPay = (Pay) object;
                Pay.Detail detail=mPay.getDetail();
                if (detail != null && detail.geteWay() != null
                        && detail.geteWay().equals("line")) {
                    //支付完成
                    if(mOnPayFinishListener!=null)mOnPayFinishListener.onPayFinish(true);
                }else{
                    PayWeixin mPayWeixin=new PayWeixin(context);
                    String data = detail.getData();
                    mPayWeixin.onPay(data);
                }
                }catch (Exception e){
                    ToastUtil.showToast(context, R.string.no_network);
                }
                break;
            case alipay:
                try {
                    Pay mPay = (Pay) object;
                    Pay.Detail detail = mPay.getDetail();
                    //0元则需要线下支付
                    if (detail != null && detail.geteWay() != null
                            && detail.geteWay().equals("line")) {
                        //支付完成
                        if(mOnPayFinishListener!=null)mOnPayFinishListener.onPayFinish(true);
                    }else{
                        PayAlipay mPayAlipay=new PayAlipay(context);
                        mPayAlipay.onRecharge(detail.getSign(), detail.getSign_type(), detail.getData());
                    }
                }catch (Exception e){
                    ToastUtil.showToast(context, R.string.no_network);
                }
                break;
            case line:
            case yue:
                try{
                    Pay mPay = (Pay) object;
                    Pay.Detail detail = mPay.getDetail();
                    if (detail != null && detail.geteWay() != null
                            &&( detail.geteWay().equals("line")|| detail.geteWay().equals("bbxpay"))) {
                        //支付完成
                        if(mOnPayFinishListener!=null)mOnPayFinishListener.onPayFinish(true);
                    }
                }catch (Exception e){
                    ToastUtil.showToast(context, R.string.no_network);
                }
                break;
        }
    }
    @Override
    public void failed(int status, String msg,String message) {
        try{
            JSONObject jmsg;
            jmsg = new JSONObject(msg);
           String detail= jmsg.getString("detail");
            JSONObject j_detail = new JSONObject(detail);
            ToastUtil.showToast(context, j_detail.getString("result"));
        }catch (Exception e){
            ToastUtil.showToast(context, message);
        }


    }
    @Override
    public BaseNetwork getBaseNetwork() {
        return new PayNet(context);
    }
}


